package com.health.memberbackstage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.memberbackstage.mapper.MemberMapper;
import com.health.memberbackstage.pojo.Member;
import com.health.memberbackstage.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService, UserDetailsService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        Member member = memberMapper.selectByName(username);

        if (member != null){
            return User
                    .withUsername(member.getId().toString())
                    .password(member.getPassword())
                    .authorities(new String[]{"DEFAULT"})
                    .build();
        }
        return null;
    }

    @Override
    public Member findByIdCard(String idcard) {
        return memberMapper.selectByIdCard(idcard);
    }
}
